package com.glsc.ngateway.opmanage.config;

import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class DatasourceConfig {

    @Resource(name = DataSourceDefConfig.DS_MYSQL)
    private DataSource datasourceMysql;

    @Resource(name = DataSourceDefConfig.DS_MYSQL_GLSC_DATA)
    private DataSource datasourceMysqlglscdata;

//    @Resource(name = DataSourceDefConfig.POSTGRE_6)
//    private DataSource datasourcePostGre6;

    @Resource(name = DataSourceDefConfig.DS_ORACLE)
    private DataSource datasourceOracle;

    @Resource(name = DataSourceDefConfig.DS_ORACLE_YONG_YOU)
    private DataSource datasourceOracleYongYou;

    @Resource(name = DataSourceDefConfig.DS_ORACLE_XIN_YI_ZJ)
    private DataSource datasourceOracleXinYiZj;

    /**
     * 多数据源，自定义mysql/oracle的连接属性
     */
    @Primary
    @Bean(name = "entityManagerFactoryMysql")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean emf = builder
                .dataSource(datasourceMysql)
                .packages("com.glsc.ngateway.common.base.domain.mysql.gateway","com.glsc.ngateway.common.base.domain.mysql.opmanagemain") //设置实体类所在位置
                .persistenceUnit("mysqlPersistenceUnit")
                .build();
        Properties properties = new Properties();
        //方言
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL55Dialect");
        //不需要自动创建、更新、验证数据库表结构
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        //输出SQL
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "true");
        //懒加载
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        emf.setJpaProperties(properties);
        return emf;
    }


//    @Bean(name = "entityManagerFactoryPostGre6")
//    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPostGre6(EntityManagerFactoryBuilder builder) {
//        LocalContainerEntityManagerFactoryBean emf = builder
//                .dataSource(datasourcePostGre6)
//                // 设置实体类所在位置，复用mysql的账户组实体类、字典实体类
//                .packages("com.glsc.ngateway.common.base.domain.postgre6")
//                .persistenceUnit("postgre6PersistenceUnit")
//                .build();
//
//        Properties properties = new Properties();
//        //方言
//        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
//        //不需要自动创建、更新、验证数据库表结构
//        properties.setProperty("hibernate.hbm2ddl.auto", "none");
//        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
//        emf.setJpaProperties(properties);
//        return emf;
//
//    }

    @Bean(name = "entityManagerFactoryMysqlglscdata")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysqlglscdata(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean emf = builder
                .dataSource(datasourceMysqlglscdata)
                .packages("com.glsc.ngateway.common.base.domain.mysql.opmanage") //设置实体类所在位置
                .persistenceUnit("mysqlglscdataPersistenceUnit")
                .build();
        Properties properties = new Properties();
        //方言
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL55Dialect");
        //不需要自动创建、更新、验证数据库表结构
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        //输出SQL
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "true");
        //懒加载
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        emf.setJpaProperties(properties);
        return emf;
    }

    @Bean(name = "entityManagerFactoryOracle")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryOracle(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean emf = builder
                .dataSource(datasourceOracle)
                .packages("com.glsc.ngateway.common.base.domain.oracle.ecology") //设置实体类所在位置
                .persistenceUnit("oraclePersistenceUnit")
                .build();
        Properties properties = new Properties();
        //方言
        //properties.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
        properties.setProperty("hibernate.dialect", "com.glsc.ngateway.opmanage.utils.CustomOracleDialect");//解决oa oracle sequence min_value过小，重载dialect

        //不需要自动创建、更新、验证数据库表结构
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        //输出SQL
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.format_sql", "true");
        //懒加载
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        emf.setJpaProperties(properties);
        return emf;
    }

    @Bean(name = "entityManagerFactoryOracleYongYou")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryOracleYongYou(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean emf = builder
                .dataSource(datasourceOracleYongYou)
                .packages("com.glsc.ngateway.common.base.domain.oracle.yongyou") //设置实体类所在位置
                .persistenceUnit("oraclePersistenceUnit")
                .build();
        Properties properties = new Properties();
        //方言
        //properties.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
        properties.setProperty("hibernate.dialect", "com.glsc.ngateway.opmanage.utils.CustomOracleDialect");//解决oa oracle sequence min_value过小，重载dialect

        //不需要自动创建、更新、验证数据库表结构
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        //输出SQL
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.format_sql", "true");
        //懒加载
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        emf.setJpaProperties(properties);
        return emf;
    }

    @Bean(name = "entityManagerFactoryOracleXinYiZj")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryOracleXinYiZj(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean emf = builder
                .dataSource(datasourceOracleXinYiZj)
                .packages("com.glsc.ngateway.common.base.domain.oracle.xinyizj") //设置实体类所在位置
                .persistenceUnit("oraclePersistenceUnit")
                .build();
        Properties properties = new Properties();
        //方言
        //properties.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
        properties.setProperty("hibernate.dialect", "com.glsc.ngateway.opmanage.utils.CustomOracleDialect");//解决oa oracle sequence min_value过小，重载dialect

        //不需要自动创建、更新、验证数据库表结构
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        //输出SQL
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.format_sql", "true");
        //懒加载
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        emf.setJpaProperties(properties);
        return emf;
    }

    @Primary
    @Bean(name = "entityManagerMysql")
    public EntityManager entityManagerMysql(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMysql(builder).getObject().createEntityManager();
    }
//
//    @Bean(name = "entityManagerMysqlPostGre6")
//    public EntityManager entityManagerMysqlPostGre6(EntityManagerFactoryBuilder builder) {
//        return entityManagerFactoryPostGre6(builder).getObject().createEntityManager();
//    }

    @Bean(name = "entityManagerMysqlglscdata")
    public EntityManager entityManagerMysqlglscdata(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMysqlglscdata(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerOracle")
    public EntityManager entityManagerOracle(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryOracle(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactoryOracleYongYou")
    public EntityManager entityManagerOracleYongYou(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryOracleYongYou(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactoryOracleXinYiZj")
    public EntityManager entityManagerOracleXinYiZj(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryOracleXinYiZj(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "transactionManagerMysql")
    public PlatformTransactionManager transactionManagerMysql(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMysql(builder).getObject());
    }

    @Bean(name = "transactionManagerMysqlglscdata")
    public PlatformTransactionManager transactionManagerMysqlglscdata(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMysqlglscdata(builder).getObject());
    }

//    @Bean(name = "transactionManagerMysqlPostGre6")
//    public PlatformTransactionManager transactionManagerMysqlPostGre6(EntityManagerFactoryBuilder builder) {
//        return new JpaTransactionManager(entityManagerFactoryPostGre6(builder).getObject());
//    }

    @Bean(name = "transactionManagerOracle")
    public PlatformTransactionManager transactionManagerOracle(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryOracle(builder).getObject());
    }

    @Bean(name = "transactionManagerOracleYongYou")
    public PlatformTransactionManager transactionManagerOracleYongYou(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryOracleYongYou(builder).getObject());
    }

    @Bean(name = "transactionManagerOracleXinYiZj")
    public PlatformTransactionManager transactionManagerOracleXinYiZj(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryOracleXinYiZj(builder).getObject());
    }

}
